package com.mapper;

import com.pojo.GroupActivity;
import com.pojo.GroupActivityVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

public interface GroupActivityMapper {


    @Select("""
        SELECT 
            ga.id AS id,
            ga.start_time AS start_time,
            ga.end_time AS end_time,
            ga.group_size AS group_size,
            ga.group_price AS group_price,
            (
                SELECT COUNT(*) 
                FROM group_join gj 
                WHERE gj.activity_id = ga.id
            ) AS current_size,
            p.id AS product_id,
            p.name AS product_name,
            p.description AS product_description,
            p.price AS product_price,
            p.stock AS product_stock,
            p.image_url AS product_image_url,
            p.category_id AS product_category_id,
            p.status AS product_status,
            p.created_at AS product_created_at
        FROM group_activity ga
        LEFT JOIN product p ON ga.product_id = p.id
        WHERE ga.id = #{id}
    """)

    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "start_time", property = "startTime"),
            @Result(column = "end_time", property = "endTime"),
            @Result(column = "group_size", property = "groupSize"),
            @Result(column = "group_price", property = "groupPrice"),
            @Result(column = "current_size", property = "currentSize"),

            // 映射 Product 对象的字段
            @Result(property = "product.id", column = "product_id"),
            @Result(property = "product.name", column = "product_name"),
            @Result(property = "product.description", column = "product_description"),
            @Result(property = "product.price", column = "product_price"),
            @Result(property = "product.stock", column = "product_stock"),
            @Result(property = "product.imageUrl", column = "product_image_url"),
            @Result(property = "product.categoryId", column = "product_category_id"),
            @Result(property = "product.status", column = "product_status"),
            @Result(property = "product.createdAt", column = "product_created_at")
    })
    GroupActivityVO selectDetailById(@Param("id") Long id);


    @Select("SELECT * FROM group_activity")
    List<GroupActivity> findAll();

    @Delete("DELETE FROM group_activity WHERE id = #{id}")
    void delete(Long id);

    @Select("SELECT * FROM group_activity WHERE product_id = #{productId} AND start_time <= #{now} AND end_time >= #{now} AND status = 0 LIMIT 1")
    GroupActivity selectActiveByProductId(@Param("productId") Long productId, @Param("now") LocalDateTime now);

    @Insert("INSERT INTO group_activity(product_id, start_time, end_time, group_size, group_price, status) " +
            "VALUES(#{productId}, #{startTime}, #{endTime}, #{groupSize}, #{groupPrice}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(GroupActivity activity);

    @Select("SELECT * FROM group_activity WHERE id = #{id}")
    GroupActivity selectById(Long id);

    @Update("UPDATE group_activity SET product_id=#{productId}, start_time=#{startTime}, end_time=#{endTime}, " +
            "group_size=#{groupSize}, group_price=#{groupPrice}, status=#{status} WHERE id=#{id}")
    void update(GroupActivity groupActivity);

    @Select("SELECT * FROM group_activity WHERE product_id = #{productId} ORDER BY id DESC LIMIT 1")
    GroupActivity selectTemplateByProductId(@Param("productId") Long productId);

}
